<?php
include_once '../Model/GateZtl.php';
include_once '../Model/GPS.php';
include_once '../Model/DateTime.php';
include_once '../AtacCommunicator/ACztl.php';
include_once 'Constants.php';
include_once '../Utility/DebugLogs.php';
include_once '../InfoManager/Constants.php';
include_once '../Model/Location.php';

class InfoZtl{
	
	public static $INFOSEPARATOR = '_';

	function __construct() {

	}
	
	
	/*Restiruisce info sul varco ZTL tramite la via  */
	
		function getGateInfo($via) {
		$info = "";
		$giorno=getNomeGiorno();
		$ora=getOrario(); 
		$conn = mysql_connect(DBHOST, DBUSER, DBPASS);

		if ($conn==FALSE)
			return NULL;
		mysql_select_db(DBNAME);
		$query = "select * from ".ZTLSTABLENAME." where VIA	='".$via."' AND GIORNO = '".$giorno."';";
		//$query="SELECT * FROM $table WHERE VIA = $via";
		$result = mysql_query($query);
		
		
		if($result==FALSE)
			return NULL;
		
		$gates = array();
		while($row = mysql_fetch_array($result))
		{		
				$gateID = $row['ID'];
				$via = $row['VIA'];
				$lat = $row['LAT'];
				$long = $row['LONG'];
				$zona = $row['ZONA'];
				$tipo = $row['TIPO'];
				$start1 = $row['START1'];
				$end1 = $row['END1'];
				$start2 = $row['START2'];
				$end2 = $row['END2'];
				$start3 = $row['START3'];
				$end3 = $row['END3'];
				
				$info = $row['ID'].InfoZtl::$INFOSEPARATOR.$row['VIA']
						.InfoZtl::$INFOSEPARATOR.$row['ZONA'].InfoZtl::$INFOSEPARATOR.$row['TIPO']
						.InfoZtl::$INFOSEPARATOR.$row['LAT'].InfoZtl::$INFOSEPARATOR.$row['LONG']
						.InfoZtl::$INFOSEPARATOR.$row['START1'].InfoZtl::$INFOSEPARATOR.$row['END1']
						.InfoZtl::$INFOSEPARATOR.$row['START2'].InfoZtl::$INFOSEPARATOR.$row['END2']
						.InfoZtl::$INFOSEPARATOR.$row['START3'].InfoZtl::$INFOSEPARATOR.$row['END3']
						.InfoZtl::$INFOSEPARATOR.$stato;
				}
		mysql_close($conn);
		return $info;
	}
		
		
/*Restiruisce info sul varco ZTL tramite la VIA  */		
	function getGateInformation($via) {
		$info = "";
		$giorno=getNomeGiorno();
		$ora=getOrario();
		$conn = mysql_connect(DBHOST, DBUSER, DBPASS);

		if ($conn==FALSE)
			return info;
			
		mysql_select_db(DBNAME);
		
		$query = "select * from ".ZTLSTABLENAME." where VIA='".$via."';";
		$result = mysql_query($query);
		
		if($result==null)
			return "";
			
		$gates = array();
		while($row = mysql_fetch_array($result))
		{
				$gateID = $row['ID'];
				$via = $row['VIA'];
				$lat = $row['LAT'];
				$long = $row['LONG'];
				$zona = $row['ZONA'];
				$tipo = $row['TIPO'];
				$start1 = $row['START1'];
				$end1 = $row['END1'];
				$start2 = $row['START2'];
				$end2 = $row['END2'];
				$start3 = $row['START3'];
				$end3 = $row['END3'];
				$stato = TimeControl($start1,$end1,$start2,$end2,$start3,$end3,$ora);
						
				$info = $row['ID'].InfoZtl::$INFOSEPARATOR.$row['VIA']
						.InfoZtl::$INFOSEPARATOR.$row['ZONA'].InfoZtl::$INFOSEPARATOR.$row['TIPO']
						.InfoZtl::$INFOSEPARATOR.$row['LAT'].InfoZtl::$INFOSEPARATOR.$row['LONG']
						.InfoZtl::$INFOSEPARATOR.$row['START1'].InfoZtl::$INFOSEPARATOR.$row['END1']
						.InfoZtl::$INFOSEPARATOR.$row['START2'].InfoZtl::$INFOSEPARATOR.$row['END2']
						.InfoZtl::$INFOSEPARATOR.$row['START3'].InfoZtl::$INFOSEPARATOR.$row['END3']
						.InfoZtl::$INFOSEPARATOR.$stato;
				}
		
		mysql_close($conn);
		return $info;
		
	} 
	
//ottiene la lista dei varchi dal db in ordine sparso
	
	function getGates() {
			$giorno=getNomeGiorno();
			$ora=getOrario();
		
			$conn = mysql_connect(DBHOST, DBUSER, DBPASS); 
			mysql_select_db(DBNAME);
			if ($conn == FALSE)
			{
				return NULL;
			}
			
			$query = "select * from ".ZTLSTABLENAME." where GIORNO='".$giorno."';";
			$result = mysql_query($query);
		   if ($result==FALSE)
				return NULL;
			$gates = array();
			
			while($row = mysql_fetch_array($result))
			{
				$gateID = $row['ID'];
				$via = $row['VIA'];
				$zona = $row['ZONA'];
				$tipo = $row['TIPO'];
				$lat = $row['LAT'];
				$long = $row['LONG'];
				$start1 = $row['START1'];
				$end1 = $row['END1'];
				$start2 = $row['START2'];
				$end2 = $row['END2'];
				$start3 = $row['START3'];
				$end3 = $row['END3'];

				$stato = TimeControl($start1,$end1,$start2,$end2,$start3,$end3,$ora);
				
//QUERY PER ORARI SETTIMANALI//////////////////////////				
			$query2 = "select * from ".ZTLORARISTABLENAME." where TIPO='".$tipo."';";
			$res = mysql_query($query2);			
			while($row2 = mysql_fetch_assoc($res)){
				$lun_gio=$row2['LUN'];
				$ven=$row2['VEN'];
				$sab=$row2['SAB'];
				$dom=$row2['DOM'];
			}
///////////////////////////////////////////////////////
				
				$gate = new GateZtl($gateID,$via,$zona,$tipo,$lat,$long,$lun_gio,$ven,$sab,$dom,$stato);
				array_push($gates,$gate);
			
			}

			mysql_close($conn);
			//print_r ($gates);
			return $gates;

	}
	
//Restituisce la lista dei varchi ordinata rispetto alla nostra posizione
	
	function getGatesCloserThan($meters, $gpsPosition) {
		$giorno=getNomeGiorno();
		$ora=getOrario();
		$limit = 10;
		if ($gpsPosition instanceOf GPS)
		{
			$conn = mysql_connect(DBHOST, DBUSER, DBPASS); 
			mysql_select_db(DBNAME);

			//Se la connessione non va a buon fine, rendi null
			if ($conn == FALSE)
			{
				return NULL;
			}
			
			$query = "select * from ".ZTLSTABLENAME." where GIORNO='".$giorno."';";
			$result = mysql_query($query);

			//Se la query non va a buon fine, rendi null
			if ($result==FALSE)
				return NULL;
					
			$gates = array();
			
			//Il calcolo viene fatto in kilometri, quindi devo trasformare i metri in km
			$r = $meters/1000;
						
			while($row = mysql_fetch_assoc($result))
			{
				
				$lat = doubleval($row['LAT']);
				$long = doubleval($row['LONG']);
		
				$gateGPS = new GPS($lat,$long);
				$distance=$gateGPS->distanceFrom($gpsPosition);
				//echo "<br>Distanza:".$busGPS->distanceFrom($gpsPosition)*1000;				
				//if(($distance=$gateGPS->distanceFrom($gpsPosition)) <= $r) {

				$gateID = $row['ID'];
				$via = $row['VIA'];
				$lat = $row['LAT'];
				$long = $row['LONG'];
				$zona = $row['ZONA'];
				$tipo = $row['TIPO'];
				$start1 = $row['START1'];
				$end1 = $row['END1'];
				$start2 = $row['START2'];
				$end2 = $row['END2'];
				$start3 = $row['START3'];
				$end3 = $row['END3'];
				$stato = TimeControl($start1,$end1,$start2,$end2,$start3,$end3,$ora);
				
			$query2 = "select * from ".ZTLORARISTABLENAME." where TIPO='".$tipo."';";
			$res = mysql_query($query2);
			
			while($row2 = mysql_fetch_assoc($res)){
				$lun_gio=$row2['LUN'];
				$ven=$row2['VEN'];
				$sab=$row2['SAB'];
				$dom=$row2['DOM'];
			}

			
					$gate = new GateZtl ($gateID, $via, $zona, $tipo, $gateGPS->getLat(), $gateGPS->getLon(),$lun_gio,$ven,$sab,$dom,$stato);
					$gate->setDistance($distance);
	
					array_push($gates,$gate);
			//	}			
						
			}
			
			mysql_close($conn);	
			//Ordino le fermate in base alla vicinanza al mio punto gps
			$sortedGates = $this->sortGates($gates,$gpsPosition);
		
			//rendo solo le prime 10
			if (count($sortedGates) > $limit)
				return array_slice($sortedGates,0,$limit);
			else
				return $sortedGates;
			
		}
		
	}
	
	//ordina i varchi per distanza
	private function sortGates($gates, $gps) {
		if (count($gates)==0) return $gates;
		if (count($gates)==1) return $gates;
		
		$high = count($gates);//print_r($gates);
  		while ($high > 0) 
  		{
    		for ($i=0;$i<$high-1;$i++)
    		{	
    				if ($gps->distanceFrom($gates[$i]->getPosition()) > $gps->distanceFrom($gates[$i+1]->getPosition())) { 
        				$tmp = $gates[$i];
        				$gates[$i] = $gates[$i+1]; 
        				$gates[$i+1] = $tmp;
      			}
    		}
    		$high = $high-1;
  		}
		
  		return $gates;
		
	}

}

?>
